/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.usermanager.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.ksplatform.base.BaseController;
import cn.com.ksplatform.base.Return;
import cn.com.ksplatform.base.SimpleReturn;
import cn.com.ksplatform.base.page.PageInfoModel;
import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.system.auth.loginmanager.controller.LoginCons;
import cn.com.ksplatform.modules.system.auth.usermanager.model.PlatUser;
import cn.com.ksplatform.modules.system.auth.usermanager.service.PlatUserService;

/**
 * ClassName:PlatUserController <br/>
 * Date: 2015年5月8日 下午4:23:43 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Controller
@RequestMapping("/modules/system/user")
public class PlatUserController extends BaseController {

	PlatUserService platUserService;

	@Resource(name = "system.modules.system.PlatUserService")
	@Override
	public void setBaseService(BaseService baseService) {
		platUserService = (PlatUserService) baseService;
		super.setBaseService(baseService);
	}

	@RequestMapping("sou")
	@ResponseBody
	public Return sou(PlatUser user) throws Exception {
		return SimpleReturn.success("操作成功",platUserService.saveOrUpdate(user));
	}

	@RequestMapping("save")
	@ResponseBody
	public Return save(PlatUser user) throws Exception {
		return SimpleReturn.success("操作成功",platUserService.save(user));
	}

	@RequestMapping("update")
	@ResponseBody
	public Return update(PlatUser user) throws Exception {
		return SimpleReturn.success("操作成功",platUserService.updateUserOnly(user));
	}

	@RequestMapping("delete")
	@ResponseBody
	public Return delete(PlatUser user) throws Exception {
		return SimpleReturn.success("操作成功",platUserService.delete(user));
	}

	@RequestMapping("page")
	@ResponseBody
	public PageInfoModel queryPage() throws Exception {
		return super.queryPage(PlatUser.class);
	}

	@RequestMapping("get/{id}")
	@ResponseBody
	public Return load(@PathVariable String id) {
		return super.load(PlatUser.class, id);
	}

	@RequestMapping("findAll")
	@ResponseBody
	public Return findAll() throws Exception {
		return super.findAll(PlatUser.class);
	}
	
	
	/**
	 * 修改密码
	 * @param opwd 原密码
	 * @param npwd 新密码
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("updatePwd")
	@ResponseBody
	public Return updatePwd(String opwd,String npwd,HttpSession session) throws Exception {
		//获取当前的登录用户
		PlatUser user = (PlatUser) session.getAttribute(LoginCons.LOGIN_USER);
		return SimpleReturn.success("操作成功",platUserService.updatePwd(opwd,npwd,user.getUserName()));
	}
}
